# 创建子进程
""""
import os
print('Process (%s) start...' % os.getpid())
pid = os.fork()
if pid == 0:
    print('I am child processing (%s) and my parent is %s.'%(os.getpid(),os.getppid()))
else:
    print('I (%s) just created a child process (%s).'% (os.getpid(),pid))
"""
#   multiprocessing    多进程

from multiprocessing import Process
import  os

from conda.notices.views import print_notices


# 子进程要执行的代码
def run_proc(name):
    print('Run child process %s(%s)...' %(name,os.getpid()))

if __name__  == '__main__':
    print('Parent process %s.'% os.getpid())
    p = Process(target=run_proc,args=('test',))
    print('Child process will start.')
    p.start()
    p.join()
    print('Child process end.')

## pool  启动大量的子进程，进程池批量创建

from multiprocessing import Pool
import os,time,random

def long_time_task(name):
    print('Run task %s (%s)...'%(name,os.getpid()))
    start = time.time()
    time.sleep(random.random() *3)
    end = time.time()
    print('Task %s runs %02f seconds.'%(name,(end-start)))

if __name__ == '__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_task,args=(i,))
    print('Waitinf for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

# subprocess 方便快速启动一个子进程，控制其输入输出

import subprocess

print('$ nslookup www.python.org')
r = subprocess.call(['nslookup','www.python.org'])
print('Exit code:',r)
"""
#  communicate() 方法输入 子进程
import subprocess
print('$ nslookup')
p = subprocess.Popen(['nslookup'],stdin = subprocess.PIPE,stdout = subprocess.PIPE,stder=subprocess.PIPE)
print(output.decode('utf-8'))
print('Exit code:',p.returncode)

"""

# 进程之间的通信  Queue
from multiprocessing import Process ,Queue
import os,time,random

# 写数据进程执行的代码
def write(q):
    print('Process to write :%s'% os.getpid())
    for value in ['A','B','C']:
        print('Put %s to queue...'% value)
        q.put(value)
        time.sleep(random.random())
 # 读取数据进程执行的代码
 def read(q):
     print('Process to read :%s' % os.getpid())
     while True:
         value = q.get(True)
         print('Get %s from queue.'% value)

if __name__ == '__main__':       #  父进程创建Queue,并传给各个子进程：
    q = Queue()
    pw = Process(target=write,args=(q,))
    pr = Process(target=read,args=(q,))

    # 启动子进程 pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束
    pr.join()
    # pr进程是死循环，无法等待结束，强行终止
    pr.terminate()









